package com.info.dpunified.defects.dataaccess.dao;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.info.dpunified.common.utils.SQLConstants;
import com.info.dpunified.defects.DefectsSelector;
import com.info.dpunified.defects.transfer.DefectsSelection;

public class DefectsSelectorDAO implements SQLConstants{
	
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

		
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	/**
	 * This method returns a grouped list of various defects submitted to 
	 * allow users to filter out search criteria 
	 * @return filter criteria
	 */
	public List<DefectsSelection> fetchCriteria()
	{
		return this.getJdbcTemplate().query(DEFECTS_SELECT_CRITERIA, new DefectsSelectorRowMapper());
	}
	
	/**
	 * This method fetches all defects submitted for a particular filter criteria
	 * @param query
	 * @return defects submitted for a particular filter criteria
	 */
	public List<DefectsSelection> fetchDefects(String query)
	{
		return this.getJdbcTemplate().query(query, new DefectsSelectorRowMapper());
	}
	
	/**
	 * This method returns a list of distinct years for which defects are submmited
	 * @return list of years
	 */
	public List<String> fetchDefectsSubmittedYears()
	{
		return this.getJdbcTemplate().queryForList(DEFECTS_SUBMITTED_YEARS, String.class);
	}
	
	/**
	 * This method fetches all defects submission status
	 * @param query
	 * @return defects submission status
	 */
	public List<DefectsSelection> fetchDefectsSubmissionStatus(String year)
	{
		return this.getJdbcTemplate().query(DEFECTS_SUBMITTED, new String[]{year}, new int[]{Types.INTEGER}, new DefectsSelectorRowMapper());
	}
	

}
